package com.nanhai.buddhist.college.module.study.controller.admin.studytime;

import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;
import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import com.nanhai.buddhist.college.module.study.controller.admin.studytime.vo.*;
import com.nanhai.buddhist.college.module.study.convert.studytime.StudyTimeConvert;
import com.nanhai.buddhist.college.module.study.dal.dataobject.studytime.StudyTimeDO;
import com.nanhai.buddhist.college.module.study.service.studytime.StudyTimeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 教学计划时间")
@RestController
@RequestMapping("/study/time")
@Validated
public class StudyTimeController {

    @Resource
    private StudyTimeService timeService;

    @PostMapping("/create")
    @ApiOperation("创建教学计划时间")

    public CommonResult<Long> createTime(@Valid @RequestBody StudyTimeCreateReqVO createReqVO) {
        return success(timeService.createTime(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新教学计划时间")
    public CommonResult<Boolean> updateTime(@Valid @RequestBody StudyTimeUpdateReqVO updateReqVO) {
        timeService.updateTime(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除教学计划时间")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "课表编号", required = true, dataTypeClass = Long.class)
    )
    public CommonResult<Boolean> deleteTime(@RequestParam("id") Long id) {
        timeService.deleteTime(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得教学计划时间")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "课表编号", required = true, dataTypeClass = Long.class)
    )
    public CommonResult<StudyTimeRespVO> getTime(@RequestParam("id") Long id) {
        StudyTimeDO time = timeService.getTime(id);
        return success(StudyTimeConvert.INSTANCE.convert(time));
    }

    @GetMapping("/list")
    @ApiOperation("获得教学计划时间列表")
    @ApiImplicitParam(name = "ids", value = "课表编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<StudyTimeRespVO>> getTimeList(@RequestParam("ids") Collection<Long> ids) {
        List<StudyTimeDO> list = timeService.getTimeList(ids);
        return success(StudyTimeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得教学计划时间分页")
    public CommonResult<PageResult<StudyTimeRespVO>> getTimePage(@Valid StudyTimePageReqVO pageVO) {
        PageResult<StudyTimeDO> pageResult = timeService.getTimePage(pageVO);
        return success(StudyTimeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/allList")
    @ApiOperation("获得教学计划时间分页")
    public CommonResult<List<StudyTimeRespVO>> getTimeAllList(@Valid StudyTimePageAllReqVO pageVO) {
        List<StudyTimeDO> list = timeService.getTimeAllList(pageVO);
        return success(StudyTimeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出教学计划时间 Excel")
    @OperateLog(type = EXPORT)
    public void exportTimeExcel(@Valid StudyTimeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<StudyTimeDO> list = timeService.getTimeList(exportReqVO);
        // 导出 Excel
        List<StudyTimeExcelVO> datas = StudyTimeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "教学计划时间.xls", "数据", StudyTimeExcelVO.class, datas);
    }

}
